<?php
/**
 * Created by leiying
 * Date: 2017/12/25
 * Time: 12:54
 */

namespace Admin\Controller;


use Think\Controller;

class LogoController extends Controller
{ /**图标列表
 * @author leiying
 */
    public function logoList(){
        if(IS_POST){

        }else{
            $lists = M('logo')->order("add_time desc")->select();
            foreach($lists as $k=>&$v){
                $v['xid'] = $k+1;
            }
            $p = I('get.p',1);
            $this->assign('p',$p);
            $this->assign('lists',$lists);
            $this->display();
        }
    }
    /**添加图标
     * @author leiying
     */
    public function logoAdd(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            //处理参数
            $data['name'] = trim($post["name"]);
            if($data['name'] == ''){
                $this->error('图标名称不能为空');
            }
            $data['code'] = trim($post['code']);
            if($data['code'] == ''){
                $this->error('图标code不能为空');
            }
            $data['sort'] = $post['sort']?$post['sort']:0;
            $data['add_time'] = time();

            //验证通过了
            $res = M('logo')->add($data);
            if($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else{
            $this->display();
        }
    }

    /**编辑图标
     * @author leiying
     */
    public function logoEdit(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){

            /*处理参数*/
            $post = I('post.');
            $id = $post["id"];
            if(empty($id)){
                $this->error("请选择需要操作的数据");
            }
            //处理参数
            $data['name'] = trim($post["name"]);
            if($data['name'] == ''){
                $this->error('图标名称不能为空');
            }

            $data['code'] = trim($post['code']);
            if($data['code'] == ''){
                $this->error('图标code不能为空');
            }
            $data['sort'] = $post['sort']?$post['sort']:0;

            $res = M('logo')->where(array('id'=>$id))->save($data);
            if(is_numeric($res)){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }else{
            $id = I('get.id');
            if($id){
                $data = M('logo')->where(array('id'=>I('get.id')))->find();
                $this->assign('data',$data);
                $p = I('get.p',1);
                $this->assign('p',$p);
                $this->display();
            }else{
                $this->error("请选择要操作的数据");
            }
        }
    }

    /**删除图标
     * @author leiying
     */
    public function logoDel(){
        if(IS_POST){
            $post_id = I('post.id');
            if(empty($post_id)){
                $this->error("请选择需要操作的数据");
            }
            $res = M('logo')->where(array('id'=>$post_id))->delete();
            if($res){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }else{
            $this->error('非法操作');
        }
    }

}